package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentPaperQuestionMapper;
import com.zhijian.medical.entity.po.AssessmentPaperQuestion;
import com.zhijian.medical.entity.vo.request.AssessmentPaperQuestionSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentPaperQuestionVo;
import com.zhijian.medical.service.AssessmentPaperQuestionService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-试卷生成的题目表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-02-01 10:35:40
 */
@Slf4j
@Service
public class AssessmentPaperQuestionServiceImpl implements AssessmentPaperQuestionService {

    @Autowired
    private AssessmentPaperQuestionMapper mapper;

    @Override
    public AssessmentPaperQuestion selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentPaperQuestionVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByAssessmentId(Long assessmentId) {
        mapper.deleteByAssessmentId(assessmentId);
    }

    @Override
    public Long insert(AssessmentPaperQuestion assessmentPaperQuestion) {
        Date now = new Date();
        assessmentPaperQuestion.setCreateTime(now);
        assessmentPaperQuestion.setUpdateTime(now);
        return mapper.insert(assessmentPaperQuestion) > 0 ? assessmentPaperQuestion.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentPaperQuestion assessmentPaperQuestion) {
        Date now = new Date();
        assessmentPaperQuestion.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentPaperQuestion) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentPaperQuestion record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AssessmentPaperQuestionSearchVo assessmentPaperQuestionSearchVo) {
        return mapper.count(assessmentPaperQuestionSearchVo);
    }

    @Override
    public List<AssessmentPaperQuestionVo> selectList(AssessmentPaperQuestionSearchVo assessmentPaperQuestionSearchVo) {
        return mapper.selectList(assessmentPaperQuestionSearchVo);
    }

    @Override
    public Page<AssessmentPaperQuestionVo> selectPage(AssessmentPaperQuestionSearchVo assessmentPaperQuestionSearchVo) {
        //先直接查总数
        int count = count(assessmentPaperQuestionSearchVo);
        //分页
        Page<AssessmentPaperQuestionVo> page = new Page<>(true, count, assessmentPaperQuestionSearchVo.getPageNum(), assessmentPaperQuestionSearchVo.getPageSize());
        assessmentPaperQuestionSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentPaperQuestionVo> responseVos = this.selectList(assessmentPaperQuestionSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentPaperQuestion> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}